﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using M2B.GestionEntretiens.DAL;
using M2B.GestionEntretiens.POCOS;

namespace M2B.GestionEntretiens.BLL
{
    /// <summary>
    /// Cette Classe décrit le service Questions
    /// </summary>
    /// <remarks>
    /// Informations de la classe : 
    /// <list type="bullet">
    /// <item name="authors">Auteur : M2B : Maher SAMMARI</item>
    /// <item name="creation date">Date de création : 01/04/2014</item>
    /// <item name="last modification">Date de dernière modification : 01/04/2014</item>
    /// </list>
    /// </remarks>
    public class QuestionsManager
    {
        #region Properties
        /// <summary>
        /// Interface unit of work
        /// </summary>
        private IUnitOfWork _unitOfWork;
        #endregion

        #region Constructeurs
        /// <summary>
        /// Constructeur ayant comme paramétre l'interface unit of work
        /// </summary>
        /// <param name="unitOfWork">Unit of Work</param>
        public QuestionsManager(IUnitOfWork unitOfWork)
        {
            _unitOfWork = unitOfWork;
        }
        #endregion

        #region Méthodes
        /// <summary>
        /// Récupération de tous les Questions
        /// </summary>
        /// <returns>Questions</returns>
        public IEnumerable<Question> GetQuestions(string entityToEnclude)
        {
            return _unitOfWork.QuestionsRepository.GetAll(entityToEnclude);
        }
        /// <summary>
        /// Récupération d'un Question par Id
        /// </summary>
        /// <param name="id">Identificateur</param>
        /// <returns></returns>
        public Question GetQuestionById(long id)
        {
            return _unitOfWork.QuestionsRepository.GetById(id);
        }
        /// <summary>
        /// Création d'un Question
        /// </summary>
        /// <param name="question">Question</param>
        public void CreateQuestion(Question question)
        {
            if (question != null)
            {
                question.DateCreation = DateTime.Now;
                question.DateModification = DateTime.Now;
                foreach (var item in question.Propositions)
                {
                    item.DateCreation = DateTime.Now;
                    item.DateModification = DateTime.Now;
                }
                _unitOfWork.QuestionsRepository.Add(question);
                _unitOfWork.Commit();
            }
        }
        /// <summary>
        /// Mise à jour d'un Question
        /// </summary>
        /// <param name="question">Question à mettre à jour</param>
        public void UpdateQuestion(Question question)
        {
            if (question != null)
            {
                Question original = _unitOfWork.QuestionsRepository.GetById(question.QuestionId);

                if (original != null)
                {
                    question.DateCreation = original.DateCreation;
                    question.DateModification = DateTime.Now;
                    foreach (var item in question.Propositions)
                    {
                        item.DateCreation = DateTime.Now;
                        item.DateModification = DateTime.Now;
                        var prop = original.Propositions.FirstOrDefault(el => el.PropositionId == item.PropositionId);
                        if (prop != null)
                        {
                            item.DateCreation = prop.DateCreation;
                        }
                    }
                    foreach (PropertyDescriptor item in TypeDescriptor.GetProperties(question))
                    {
                        item.SetValue(original, item.GetValue(question));
                    }

                    SaveQuestions();
                }

            }
        }
        /// <summary>
        /// Suppression d'un Question
        /// </summary>
        /// <param name="id">Identificateur</param>
        public void DeleteQuestion(long id)
        {
            var questionToDelete = GetQuestionById(id);
            if (questionToDelete != null)
            {
                _unitOfWork.QuestionsRepository.Delete(questionToDelete);
                _unitOfWork.Commit();
            }

        }
        /// <summary>
        /// Sauvegarde et Commit
        /// </summary>
        public void SaveQuestions()
        {
            _unitOfWork.Commit();
        }
        #endregion

    }
}
